package scala40rules;


public class Tris extends CardSet {
	
	private CardSet myTris;
	
	public Tris(CardSet cardSet){
		//System.out.println("[Tris || new Tris()] cardSet.size()"+cardSet.size());
		
		this.myTris = cardSet;
	}
	

	public boolean isValidTris() {
//      System.out.println("[Tris || isValidTris()] isSameSymbol="+isSameSymbol());
//		System.out.println("[Tris || isValidTris()] isDifferentSuit="+isDifferentSuit());
//	    System.out.println("[Tris || isValidTris()] isAllJokers="+isAllJokers());
		if( isTris()){
            if(isSameSymbol()){
                if(isDifferentSuit()){
                    if(!isAllJokers()){
                        return true;
                    }
                }
            }
		}
		return false;
	}
	
	
	private boolean isAllJokers() {
		boolean result = (
					(myTris.getCard(0).getOrdinalValue()==14)
					&&
					(myTris.getCard(1).getOrdinalValue()==14)
					&&
					(myTris.getCard(2).getOrdinalValue()==14)
				);
		return result;
	}


	private boolean isDifferentSuit(){
		boolean result;
		result = (
				((!myTris.getCard(0).getSuit().equals(myTris.getCard(1).getSuit())) || (myTris.getCard(0).isJoker()))
				&&
				((!myTris.getCard(1).getSuit().equals(myTris.getCard(2).getSuit())) || (myTris.getCard(1).isJoker()))
				&&
				((!myTris.getCard(2).getSuit().equals(myTris.getCard(0).getSuit())) || (myTris.getCard(2).isJoker()))
				);
		//if(result){System.out.println("[Tris] isDifferentSuit = TRUE");}else{System.out.println("[Tris] isDifferentSuit = FALSE");}
		return result;
	}
	
	
	private boolean isSameSymbol(){
		boolean result;
		int ov0 = myTris.getCard(0).getOrdinalValue();
		int ov1 = myTris.getCard(1).getOrdinalValue();
		int ov2 = myTris.getCard(2).getOrdinalValue();
		result = (	
					(ov0==ov1 || ov0==14 || ov1==14)
					&&
					(ov1==ov2 || ov1==14 || ov2==14)
					&&
					(ov2==ov0 || ov2==14 || ov0==14)
				 );
		//if(result){System.out.println("[Tris] isSameSymbol = TRUE");}else{System.out.println("[Tris] isSameSymbol = FALSE");}
		return result;
	}


	private boolean isTris(){
		return (this.myTris.size() == 3);
	}

	//TODO scrivere metodo che ritorna il TIPO del cardset (tris,poker,scala)
	
	//TODO [grafica] qnd creo un tris con la matta devo selezionare il SEME, per dichiararla!
	//(roba grafica....qnd sono qui ho gia' tutto settato)
	
	private boolean isReplaceable(Card currentCard, Card card){
		if( currentCard.isJoker()){
			return currentCard.getSuit().equalsIgnoreCase(card.getSuit());
		}
		else{
			return false;
		}
	}


	public CardSet tryToSubstitute(Card card){
		int i=0;
		boolean found=false;
		Card cardOutput = new Card();
		
//		for(int j=0; j<myTris.size(); j++){
//			System.out.println(" TRIS "+ myTris.getCard(j).getSymbol()+" "+myTris.getCard(j).getSuit());
//		}
		
		if(this.existJoker()){
			while((i<myTris.size() && !found)){
//				System.out.println(" while ---  i="+i+" found="+found);
				Card currentCard = myTris.getCard(i);
				if( this.isReplaceable(currentCard, card)) {
					cardOutput = myTris.removeCard(i);
					myTris.addCard(card);
					if(this.isValidTris()){
						found=true;
					}else{
						
						myTris.removeCard(2);
						myTris.addCard(cardOutput);
					}
				}
				i++;
			}
		}
		if(found)
			return myTris;
		else
			//per notificare la mossa non effettuabile
			return null;
		
	}


	public CardSet tryToAdd(Card card){
		CardSet cardSet = new CardSet();
		for(int i=0; i<myTris.size(); i++){
			cardSet.addCard(myTris.getCard(i));
		}
		cardSet.addCard(card);
		Quatris quatris = new Quatris(cardSet);
		if(quatris.isValidQuatris()){
			return cardSet;
		}
        return null;
	}


	public CardSet getMyTris() {
		return this.myTris;
	}


	public void setMyTris(CardSet myTris) {
		this.myTris = myTris;
	}


	//TODO non ci capisco una favella!!!!!!!!!!!!!
	public int sumTrisValue(){
		for(int i=0; i<3; i++){ //se le prime due sono matte
			int j = this.myTris.getCard(i).getOrdinalValue();
			switch(j){
			case 1: return 33; 
			case 2: 
			case 3: 
			case 4: 
			case 5: 
			case 6: 
			case 7:
			case 8:
			case 9:
			case 10: return (j*3);
			case 11:
			case 12:
			case 13: return 30;
			}
		}
		return 3000;

	}


	public int size(){
		return this.myTris.size();
	}


	public boolean existJoker(){
//		System.out.println("[Tris || existJoker()] this.myTris.size()"+this.myTris.size());
		for (int i=0; i<this.myTris.size(); i++){
//			System.out.println("                                      "+i);
//			System.out.println("      [Tris || existJoker()] Carta: "+this.myTris.getCard(i).getSymbol());
			if(this.myTris.getCard(i).isJoker()){
				return true;
			}
		}
		return false;
	}


	public boolean existJokerWithoutSuit(){
//		System.out.println("[Tris || existJoker()] this.myTris.size()"+this.myTris.size());
		for (int i=0; i<this.myTris.size(); i++){
//			System.out.println("                                      "+i);
//			System.out.println("      [Tris || existJoker()] Carta: "+this.myTris.getCard(i).getSymbol());
			if(this.myTris.getCard(i).isJoker() && this.myTris.getCard(i).getSuit().equalsIgnoreCase(" ")){
				return true;
			}
		}
		return false;
	}

	
    public void printTris(){
        this.printTris("NESSUN TITOLO");
    }


    public void printTris(String titolo){
        int symbolLenght = 8;
        int suitLenght = 8;
        int valueLenght = 7;
        System.out.println("\n\nprintScale: "+titolo);
        System.out.println("[ SYMBOL |  SUIT  | VALUE ]");
        System.out.println("---------------------------");
        for(int i=0; i<this.myTris.size(); i++){
            System.out.print("[");
            System.out.print(getSpaces((symbolLenght - this.myTris.getCard(i).getSymbol().length())/2));
            System.out.print(this.myTris.getCard(i).getSymbol());
            System.out.print(getSpaces((symbolLenght - this.myTris.getCard(i).getSymbol().length())/2 + (symbolLenght - this.myTris.getCard(i).getSymbol().length())%2));
            System.out.print("|");
            System.out.print(getSpaces((suitLenght - this.myTris.getCard(i).getSuit().length())/2));
            System.out.print(this.myTris.getCard(i).getSuit());
            System.out.print(getSpaces((suitLenght - this.myTris.getCard(i).getSuit().length())/2 + (suitLenght - this.myTris.getCard(i).getSuit().length())%2));
            System.out.print("|");
            System.out.print(getSpaces((valueLenght - Integer.toString(this.myTris.getCard(i).getOrdinalValue()).length())/2));
            System.out.print(this.myTris.getCard(i).getOrdinalValue());
            System.out.print(getSpaces((valueLenght - Integer.toString(this.myTris.getCard(i).getOrdinalValue()).length())/2 + (valueLenght - Integer.toString(this.myTris.getCard(i).getOrdinalValue()).length())%2));
            System.out.println("]");
        }
        System.out.println("\n");
    }

}

